package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.DetailActivity;
import flc.ast.activity.HighlightsActivity;
import flc.ast.activity.HotActivity;
import flc.ast.adapter.HighlightsAdapter;
import flc.ast.adapter.HotAdapter;
import flc.ast.adapter.LikeAdapter;
import flc.ast.databinding.FragmentAnimationBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link AnimationFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class AnimationFragment extends BaseNoModelFragment<FragmentAnimationBinding> {

    private HighlightsAdapter highlightsAdapter;
    private HotAdapter hotAdapter;
    private LikeAdapter likeAdapter;


    @Override
    protected int onCreate() {
        return R.layout.fragment_animation;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        //每日精选
        mDataBinding.rvHighlightsList.setLayoutManager(new GridLayoutManager(mContext, 3));
        highlightsAdapter = new HighlightsAdapter();
        mDataBinding.rvHighlightsList.setAdapter(highlightsAdapter);
        highlightsAdapter.setOnItemClickListener(this);
        //热门推荐
        mDataBinding.rvHotList.setLayoutManager(new GridLayoutManager(mContext, 2));
        hotAdapter = new HotAdapter();
        mDataBinding.rvHotList.setAdapter(hotAdapter);
        hotAdapter.setOnItemClickListener(this);
        //大家都喜欢
        mDataBinding.rvLikeList.setLayoutManager(new LinearLayoutManager(mContext));
        likeAdapter = new LikeAdapter();
        mDataBinding.rvLikeList.setAdapter(likeAdapter);
        likeAdapter.setOnItemClickListener(this);

        mDataBinding.tvHighlightsMore.setOnClickListener(this);
        mDataBinding.tvHotMore.setOnClickListener(this);
        mDataBinding.tvLikeRefresh.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvHighlightsMore://每日精选查看更多
                startActivity(HighlightsActivity.class);
                break;
            case R.id.tvHotMore://热门推荐查看更多
                startActivity(HotActivity.class);
                break;
            case R.id.tvLikeRefresh://大家都喜欢换一批
                getLikeData();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        DetailActivity.bean = bean;
        startActivity(DetailActivity.class);
    }

    @Override
    protected void initData() {
        getHighlightsData();
        getHotData();
        getLikeData();
    }

    private void getHighlightsData() {
        StkResApi.getTagResourceList(null, AppConfig.HIGHLIGHTS_DATA, StkResApi.createParamMap(0, 3), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        highlightsAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void getHotData() {
        StkResApi.getTagResourceList(null, AppConfig.HOT_DATA, StkResApi.createParamMap(0, 4), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        hotAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void getLikeData() {
        StkResApi.getTagResourceList(null, AppConfig.LIKE_DATA, StkResApi.createParamMap(0, 3), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        likeAdapter.setList(result);
                    }
                }
            }
        });
    }

}